/*
 * @Author: your name
 * @Date: 2019-11-01 10:17:50
 * @LastEditTime: 2019-11-01 17:14:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \koa-example\src\controllers\public.js
 */

const cheerio = require('cheerio');
const superagent = require('superagent');
const charset = require('superagent-charset');
charset(superagent);

// 抓取百度新闻列表
async function publicNewsListController() {
    var url = 'https://news.baidu.com'; //百度新闻地址
    let result =  await superagent.get(url).then((sres) => {
        let html = sres.text,
        $ = cheerio.load(html, {
            decodeEntities: false
        }), //用cheerio解析页面数据
        news = {
            hotNews:{
                title: "热点要闻",
                list:[]
            }, // 热点要闻
            focusNews:{
                title: "聚焦新闻",
                list:[]
            }, // 聚焦新闻
            baijiaNews:{
                title: "百家号新闻",
                list:[]
            }, // 百家号新闻
            localNews:{
                title: "本地新闻",
                list:[]
            }, // 本地新闻
            inlanNews:{
                title: "国内新闻",
                list:[]
            } // 国内新闻,

            
        };

        // hot
        $(".hotnews ul li").each((index, element) => {
            var $text = $(element).text();
            var $link = $(element).find("a").attr("href");
            news.hotNews.list.push({
                title: $text,
                link: $link
            });
        });

        // focus
        $(".focuslistnews").each((index, parent) => {
            $(parent).find("li").each((index, child) => {
                var $text = $(child).text();
                var $link = $(child).find("a").attr("href");
                news.focusNews.list.push({
                    title: $text,
                    link: $link
                });
            });
        });

        // 百家号
        $(".baijia-focus-list ul").each((index, parent) => {
            $(parent).find("li").each((index, child) => {
                var $text = $(child).text();
                var $link = $(child).find("a").attr("href");
                news.baijiaNews.list.push({
                    title: $text,
                    link: $link
                });
            });
        });

        // 本地新闻
        $(".mod-localnews .ulist").each((index, parent) => {
            $(parent).find("li").each((index, child) => {
                var $text = $(child).text();
                var $link = $(child).find("a").attr("href");
                news.localNews.list.push({
                    title: $text,
                    link: $link
                });
            });
        });
        
        // 国内新闻
        $("#guonei ul").each((index, parent) => {
            $(parent).find("li").each((index, child) => {
                var $text = $(child).text();
                var $link = $(child).find("a").attr("href");
                news.inlanNews.list.push({
                    title: $text,
                    link: $link
                });
            });
        });

        return news

    }).catch(err => {
        console.error("publicNewsHotListController: ",err)
    });
  
    return result

}

// 抓取新闻详情页
async function publicNewsDetailController(url) {
    if(!url) {
        return {
            success:false,
            err_code: 000002,
            err_msg: "url 参数为必传项"
        }
    } 
    let result =  await superagent.get(url).then((sres) => {
        let html = sres.text,
        $ = cheerio.load(html, {
            decodeEntities: false
        }), //用cheerio解析页面数据
        obj = {};
        // let res = [];
        //下面类似于jquery的操作，前端的小伙伴们肯定很熟悉啦
        obj = {
            title: $(".cnt_bd h1").text(),
            origin: $(".cnt_bd .info i").text(),
            content: $(".cnt_bd content_18313").html(),
        }
        return obj

    }).catch(err => {
        console.error("publicNewsDetailController", err)
    });
  
    return result

}

module.exports = {
    publicNewsListController,
    publicNewsDetailController
}